﻿using PrinterMonitorSvc.Common;
using PrinterMonitorSvc.Core;
using PrinterMonitorSvc.Core.Enum;
using PrinterMonitorSvc.Model;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PrinterMonitorSvc.Controller
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    public class ControllerBase
    {
        protected HttpListenerRequest request;
        protected HttpListenerResponse response;
        protected LogModel logModel = new LogModel();

        public ControllerBase(HttpListenerRequest request, HttpListenerResponse response)
        {
            this.request = request;
            this.response = response;
            var requestId = HeaderString["id"];
            if (null != requestId)
            {
                this.logModel.RequestId = requestId;
            }
        }

        /// <summary>
        /// 请求头参数
        /// </summary>
        protected NameValueCollection HeaderString => request.Headers;

        /// <summary>
        /// URL参数
        /// </summary>
        protected NameValueCollection QueryString => HttpUtility.ParseQueryString(request.Url.Query, Encoding.GetEncoding("UTF-8"));

        /// <summary>
        /// Body参数
        /// </summary>
        protected string BodyString
        {
            get
            {
                using (StreamReader reader = new StreamReader(request.InputStream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}
